package q.test0421library.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import q.test0421library.Moudle.BookInfo;
import q.test0421library.Moudle.PageRequest;
import q.test0421library.Moudle.PageResult;
import q.test0421library.Moudle.Result;
import q.test0421library.Service.BookService;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    private static final Logger log = LoggerFactory.getLogger(BookController.class);
    @Autowired
    private BookService service;
    @RequestMapping("/addBook")
    public String addBook(BookInfo info){
//        if(info==null||
//                !StringUtils.hasLength(info.getBookName())||
//                !StringUtils.hasLength(info.getAuthor())||
//                !StringUtils.hasLength(info.getPublish())||
//                info.getCount()==null||
//                info.getPrice()==null) return "输入参数错误，请检查输入参数";
//
//        String s=service.addBook(info);
//        return s;

        return "测试一下";
    }


    @RequestMapping("/getListByPage")
    public Result<BookInfo> getListByPage(PageRequest request){

        log.info("获取图书列表, pageRequest:{}", request);
        PageResult<BookInfo> result=service.getListByPage(request);

        return Result.success(result);

    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if(bookId<=0||bookId==null) return null;
        BookInfo bookInfo=service.queryBookById(bookId);
        return bookInfo;

    }

@RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo){

    log.info("更新图书信息: {}", bookInfo);  // 检查 id 是否非空

    if(bookInfo.getId()==null) return Result.falt("id 为空");
        String respone=service.updateBook(bookInfo);
        return Result.success(respone);
    }


    @RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids){
       return service.batchDeleteBook(ids);

    }
}
